package com.yuzhihao.writejvm.attributes.stack;

import com.yuzhihao.writejvm.clazz.ClassInfo;
import com.yuzhihao.writejvm.format.StackMapFrameEnum;
import com.yuzhihao.writejvm.read.Parse;
import com.yuzhihao.writejvm.read.ReadClassFileFatory;
import lombok.Data;
import lombok.extern.log4j.Log4j2;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @see  <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.4">jvmdoc</a>
 * @author yzh
 * @since 2021年12月14日13:36:27
 */
@Data
@Log4j2
public class StackMapFrame implements Parse {

    private int frame_type_u1 = 1;

    protected int frame_type;

    protected List<StackMapFrame> frames = new ArrayList<>();

    private int count;

    protected ClassInfo ci;

    protected StackMapFrameEnum smfe;

    public StackMapFrame() {
    }

    public StackMapFrame(int count) {
        this.count = count;
    }

    @Override
    public void parse(DataInputStream dis, ClassInfo ci) {
        try {
           int i = 0;
           while (i < this.count){
               int value = Parse.byteToInt(this.frame_type_u1,dis);
               StackMapFrame frame = ReadClassFileFatory.smf_instance(value);
               frame.setFrame_type(value);
               frame.parse(dis, ci);
               frame.setFrames(frames);
               this.frames.add(frame);
               i++;
           }
        } catch (IOException e) {
            log.error(e);
        }
    }

    public String print(String print){
        return "";
    }

}
